#!/bin/bash

# Flutter多环境构建脚本

echo "📦 Flutter多环境构建脚本"
echo ""

# 检查参数
if [ $# -eq 0 ]; then
    echo "用法: ./build_env.sh [环境名] [平台] [可选的flutter build参数]"
    echo ""
    echo "可用环境:"
    echo "  dev   - 开发环境"
    echo "  test  - 测试环境"
    echo "  prod  - 生产环境"
    echo ""
    echo "可用平台:"
    echo "  apk      - Android APK"
    echo "  appbundle - Android App Bundle"
    echo "  ios      - iOS"
    echo "  web      - Web"
    echo "  macos    - macOS"
    echo "  windows  - Windows"
    echo "  linux    - Linux"
    echo ""
    echo "示例:"
    echo "  ./build_env.sh dev apk"
    echo "  ./build_env.sh prod appbundle --release"
    echo "  ./build_env.sh test web --release"
    exit 1
fi

ENV=$1
PLATFORM=$2
shift 2  # 移除前两个参数，剩下的作为flutter build的参数

# 验证环境
case $ENV in
    "dev"|"test"|"prod")
        ;;
    *)
        echo "❌ 未知环境: $ENV"
        echo "可用环境: dev, test, prod"
        exit 1
        ;;
esac

# 验证平台
case $PLATFORM in
    "apk"|"appbundle"|"ios"|"web"|"macos"|"windows"|"linux")
        ;;
    *)
        echo "❌ 未知平台: $PLATFORM"
        echo "可用平台: apk, appbundle, ios, web, macos, windows, linux"
        exit 1
        ;;
esac

echo "🏗️  构建环境: $ENV"
echo "📱 目标平台: $PLATFORM"
echo ""

# 构建应用
flutter build $PLATFORM --dart-define=ENV=$ENV "$@"

if [ $? -eq 0 ]; then
    echo ""
    echo "✅ 构建成功!"
    echo "📦 环境: $ENV"
    echo "📱 平台: $PLATFORM"
else
    echo ""
    echo "❌ 构建失败!"
    exit 1
fi
